package com.gxa.p2p.controller;

import com.gxa.p2p.businessExceptionCode.ErrorCode;
import com.gxa.p2p.exception.BusinessException;
import com.gxa.p2p.pojo.CertificateMaterial;
import com.gxa.p2p.service.CertificateMaterialService;
import com.gxa.p2p.util.FileUploadUtil;
import com.gxa.p2p.util.MapUtil;
import com.gxa.p2p.util.ReturnMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @description
 * @date 2020.8.1
 */
@CrossOrigin
@RequestMapping("/certificateMaterial")
@Controller("certificateMaterialController")
@ResponseBody
@Api(value = "风控认证的控制器", tags = "风控认证模块")
public class CertificateMaterialController {

    @Resource(name = "certificateMaterialService")
    CertificateMaterialService certificateMaterialService;

    /**
     * 查询数据库所有数据 可带条件查询
     * @param paramMap
     * @return Map
     */
    @PostMapping
    @ApiOperation(value = "查询所有数据的接口,可输入状态,时间范围这两个条件", response = Map.class, httpMethod = "GET")
    public Map<String,Object> findAll(@RequestBody @ApiParam(value = "参数map,包含状态,时间范围,分页参数", required = true, type = "MAP") Map<String,Object> paramMap) {
        MapUtil mapUtil = new MapUtil();
        List<CertificateMaterial> list = certificateMaterialService.findAll(paramMap);
        Integer dataSum = certificateMaterialService.dataSum(paramMap);
        if (dataSum > 0) {
            mapUtil.put("data",list);
            mapUtil.put("count",dataSum);
            mapUtil.setCode(0);
        } else {
            throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /**
     * 追加审核信息(改) 添加到该条记录上
     * @param certificateMaterial
     * @return Map
     */
    @PutMapping
    @ApiOperation(value = "追加审核信息的接口")
    public Map<String,Object> updateReviewStatus(@RequestBody @ApiParam(value = "certificateMaterial对象,包含该风控认证的所有信息", required = true, type = "CertificateMaterial") CertificateMaterial certificateMaterial) {
        MapUtil mapUtil = new MapUtil();
        Integer lines = certificateMaterialService.updateReviewStatus(certificateMaterial);
        if (lines > 0)
            mapUtil.setCode(0);
        else
            throw new BusinessException(ErrorCode.FAILED);
        return mapUtil.getMap();
    }
    @ApiOperation(value = "风控资料查询接口",notes = "风控资料查询接口",
            httpMethod = "GET",response = CertificateMaterial.class)
    @ApiImplicitParam(name = "id", value = "需要查询信息用户的id",
            required = true, dataType = "int")
    @ResponseBody
    @GetMapping("/findById")
    public Map<String, Object> selectById(@RequestParam("userId") Integer id) {
        CertificateMaterial certificateMaterial = certificateMaterialService.selectById(id);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(certificateMaterial);
    }
    /**
     * 风控资料保存方法
     *
     */
    @ApiOperation(value = "风控资料保存接口",
            notes = "风控资料保存接口",
            httpMethod = "Post", response = Map.class)
    @ResponseBody
    @PostMapping("/saveCer")
    public Map<String, Object> saveCer(@RequestBody @ApiParam("传入json字符串") CertificateMaterial certificateMaterial){
        MapUtil mapUtil = new MapUtil();
        if (certificateMaterialService.saveCer(certificateMaterial)){
            mapUtil.setCode(0);
            mapUtil.setMsg("成功");
        }else {
           throw new BusinessException(ErrorCode.FAILED);
        }
        return mapUtil.getMap();
    }

    /*
     * 风控资料图片上传
     */
    @ApiOperation(value = "风控图片上传接口",
            notes = "风控图片保存接口",
            httpMethod = "Post", response = List.class)
    @ApiImplicitParam(name = "file", value = "上传文件的FormData名字",
            required = true, dataType = "MultipartFile")
    @ResponseBody
    @PostMapping("/ImgUpload")
    public Map<String, Object> upLoad(@RequestParam("file") List<MultipartFile> multipartFileList, HttpServletRequest request) throws IOException {
        List<Map<String, Object>> list = FileUploadUtil.upload(multipartFileList, request);
        ReturnMap<Object> returnMap = new ReturnMap<>();
        return returnMap.returnMap(list);
    }

}
